package cn.jiahua.hospital.controller;

import cn.jiahua.hospital.constants.ResultEnum;
import cn.jiahua.hospital.dto.DoctorScheduleDto;
import cn.jiahua.hospital.dto.PageDepartmentDto;
import cn.jiahua.hospital.dto.PageDoctorDto;
import cn.jiahua.hospital.dto.Result;
import cn.jiahua.hospital.pojo.Tdepartments;
import cn.jiahua.hospital.service.DoctorService;
import cn.jiahua.hospital.service.ScheduleService;
import cn.jiahua.hospital.service.TdepartmentsService;
import cn.jiahua.hospital.utils.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@Api
@RestController
@RequestMapping("api/tdepartments")
public class tdepartmentsController {

    @Resource
    private TdepartmentsService tdepartmentsService;

    @Resource
    private DoctorService doctorService;

    @Resource
    private ScheduleService scheduleService;


    @GetMapping("getTdepartmentsByCondition")
    @ApiOperation(value="获取模糊查询科室id",notes = "根据医院id和科室id获取医院的科室id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tdepartmentsName",value = "科室名称"),
            @ApiImplicitParam(name = "hospitalId",value = "医院id")
    })
    public Result getTdepartmentsByCondition(@RequestParam(value = "tdepartmentsName",required = true)String tdepartmentsName,
                                             @RequestParam(value = "hospitalId",required = true)Integer hospitalId){
        int tdId = 0;
        if (tdepartmentsName == null || tdepartmentsName == "" || hospitalId == null) {
            return ResultUtils.returnFail(ResultEnum.FAIL_PARAM.getCode(), ResultEnum.FAIL_PARAM.getMsg());
        }
        try {
            List<Integer> tdepartments = tdepartmentsService.getTdepartmentsByCondition(hospitalId, tdepartmentsName);
            if (tdepartments==null || tdepartments.size()<=0){
                return ResultUtils.returnFail(ResultEnum.FAIL_HAVE_NOT_EXIST.getMsg(),ResultEnum.FAIL_HAVE_NOT_EXIST.getCode());
            }
            tdId = tdepartments.get(0);
        } catch (Exception e) {
            e.printStackTrace();
            return  ResultUtils.returnFail("出现异常,查看日志");
        }
        return ResultUtils.returnDataSuccess(tdId);
    }

    @GetMapping("getPageTdepartmentsInfo")
    @ApiOperation(value = "获取前台科室页面",notes = "根据医院id和科室id获取科室页面的详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "hospitalId",value = "医院id"),
            @ApiImplicitParam(name = "tdepartmentsId",value = "科室id")
    })
    public Result getPageTdepartmentsInfo(@RequestParam(value = "hospitalId")Integer hospitalId,
                                          @RequestParam(value = "tdepartmentsId")Integer tdepartmentsId){
        PageDepartmentDto pageDepartment = null;
        if (tdepartmentsId == null ||  hospitalId == null) {
            return ResultUtils.returnFail(ResultEnum.FAIL_PARAM.getCode(), ResultEnum.FAIL_PARAM.getMsg());
        }
        try {
            //获取科室信息
            pageDepartment = tdepartmentsService.getPageDepartment(hospitalId, tdepartmentsId);
            if (pageDepartment==null){
                return ResultUtils.returnFail(ResultEnum.FAIL_HAVE_NOT_EXIST.getMsg(),ResultEnum.FAIL_HAVE_NOT_EXIST.getCode());
            }
            //获取科室中的医生信息
            List<PageDoctorDto> doctorPageInfo = doctorService.getDoctorPageInfo(null, hospitalId, tdepartmentsId);
            if (doctorPageInfo==null || doctorPageInfo.isEmpty()){
                return ResultUtils.returnFail(ResultEnum.FAIL_HAVE_NOT_EXIST.getMsg(),ResultEnum.FAIL_HAVE_NOT_EXIST.getCode());
            }
            for (PageDoctorDto pageDoctorDto:doctorPageInfo){
                //获取每一个医生的排版信息
                List<DoctorScheduleDto> doctorSchedule = scheduleService.getDoctorSchedule(pageDoctorDto.getDoctorId());
                if (doctorPageInfo==null || doctorPageInfo.isEmpty()){
                    continue;
                }
                pageDoctorDto.setDoctorScheduleDtos(doctorSchedule);
            }
            pageDepartment.setPageDoctorDtos(doctorPageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return  ResultUtils.returnFail("出现异常,查看日志");
        }

        return  ResultUtils.returnDataSuccess(pageDepartment);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "hospitalId", value = "医院ID", required = true, dataType = "int"),
            @ApiImplicitParam(name = "departmentsId", value = "医院ID", required = true, dataType = "int")
    })
    @ApiOperation(value="ID查询二级科室")
    @GetMapping("select/hospital/tdepartments")
    public Result selectTdepartments(Integer hospitalId,Integer departmentsId){
      List<Tdepartments>  listTdepartments=tdepartmentsService.selectTdep(hospitalId,departmentsId);
        return  ResultUtils.returnDataSuccess(listTdepartments);
    }

}
